package model.conditions;

import metier.GameState;
import metier.Objet;

public class CharacterTouchCondition extends Condition {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8616777106943359836L;

	public CharacterTouchCondition(boolean inverse) {
		super();
		this.inverse = inverse;
	}
	
	public CharacterTouchCondition() {
		super();
	}

	@Override
	public boolean verify(GameState state, Objet sender) {
		boolean res = false;
		
		if(sender!=state.getPersonnage()) {
			res = verifyPersonnage(state, sender);
		} else {
			res = verifyEnemies(state, sender);
		}
		
		if(!inverse) {
			return res;
		} else {
			return !res;
		}
	}

	private boolean verifyPersonnage(GameState state, Objet sender) {
		int c = sender.getPosition().getColumn();
		int r = sender.getPosition().getRow();
		int cPers = state.getPersonnage().getPosition().getColumn();
		int rPers = state.getPersonnage().getPosition().getRow();
		if(c == cPers && (rPers == r || rPers == r-1 || rPers == r+1 )) {
			return true;
		}
		if(r == rPers && (cPers == c || cPers == c-1 || cPers == c+1 )) {
			return true;
		}
		return false;
	}
	
	private boolean verifyEnemies(GameState state, Objet sender) {
		int c = state.getPersonnage().getPosition().getColumn();
		int r = state.getPersonnage().getPosition().getRow();
		for(Objet enem : state.getObjects()) {
			int cEnem = enem.getPosition().getColumn();
			int rEnem = enem.getPosition().getRow();
			if(c == cEnem && (rEnem == r || rEnem == r-1 || rEnem == r+1 )) {
				return true;
			}
			if(r == rEnem && (cEnem == c || cEnem == c-1 || cEnem == c+1 )) {
				return true;
			}
		}
		return false;
	}

}
